<template>
  <div>
    <button @click="add">新增</button>
    <transition-group name="student" tag="ul">
      <li v-for="(item, index) in students" :key="item.id">
        name:{{ name }}---age:{{ age }}
        <em @click="deleteStudent(index)">删除</em>
      </li>
    </transition-group>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      students: [
        {
          id: '001',
          name: '张三',
          age: 18,
        },
        {
          id: '002',
          name: '李四',
          age: 20,
        },
        {
          id: '003',
          name: '王五',
          age: 22,
        },
        {
          id: '004',
          name: '赵六',
          age: 24,
        },
        {
          id: '005',
          name: '孙七',
          age: 26,
        },
      ],
    };
  },
  methods: {
    add() {
      this.students.unshift({ id: '006', name: '老八', age: 30 });
    },
    deleteStudent(index) {
      this.students.splice(index, 1);
    },
  },
};
</script>
<style scoped>
ul {
  width: 400px;
}
ul li {
  height: 40px;
  width: 400px;
  line-height: 40px;
  border: 1px solid #000;
}

.student-enter,
.student-leave-to {
  opacity: 0;
}
.student-enter-active,
.student-leave-active {
  transition: all 2s;
}

/* xxx-move类  是控制列表中其他元素移动时的过渡动画 */
.student-move {
  transition: all 2s;
}

/* 对于删除元素而言，我们不让他占用空间，我们就可以move起作用了 */
.student-leave-active {
  position: absolute;
}
</style>
